package xyc.szk.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils2 {
    private static DataSource ds = null;

    static {
        //将jdbc.properties这个配置文件中key-value读到一个properties对象中
        Properties p = new Properties();
        //用类加载器来读取配置文件jdbc.properties
        ClassLoader cl = JdbcUtils2.class.getClassLoader();
        InputStream in = null;
        try {

           /* in = cl.getResourceAsStream("jdbc.properties");
            p.load(in);
            ComboPooledDataSource bds = new ComboPooledDataSource();*/
           /* bds.setDriverClass(p.getProperty("jdbc.driver"));
            bds.setJdbcUrl(p.getProperty("jdbc.url"));
            bds.setUser(p.getProperty("jdbc.username"));
            bds.setPassword(p.getProperty("jdbc.password"));*/
            ComboPooledDataSource bds = new ComboPooledDataSource();
            //ComboPooledDataSource bds = new ComboPooledDataSource("mysql");
            ds = bds;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
    public  static DataSource getDataSource(){
        return ds;
    }
}
